<?php
namespace app\api\controller;

use app\common\controller\IndexBaseController;
use think\Db;
use app\adminmall\model\AddressModel as AddressModel;

/**
 * 我的列表
 */

class AddressController extends IndexBaseController {

	protected $Address_model;

	protected function _initialize() {
		parent::_initialize();
		$this -> Address_model = new AddressModel();
	}

	/**
	 * 地址列表
	 */
	public function index() {
		$list = $this -> Address_model -> where('uid', input('uid')) -> select();
		return $this -> toJson('200', '', $list);
	}
	public function getAddress(){
		$address=$this->Address_model->find(input("sqd"));
		return $this->toJson('200','',$address);
	}
	/**
	 * 添加地址
	 */
	public function add() {
			$data = $this -> request -> post();
			dump($data);
			$count = $this -> Address_model -> where('uid', $data['uid']) -> count();
			if ($count >= 5) {
				return $this -> toJson('408', '添加地址最多为5个');
			}
			if ($this -> Address_model -> allowField(true) -> save($data)) {
				return $this -> toJson('200', '添加成功');
			} else {
				return $this -> toJson('406', '添加失败');
			}
	}

	/**
	 * 修改地址
	 */
	public function update() {
		if ($this -> request -> isPost()) {
			$data = $this -> request -> post();
			if ($this -> Address_model -> allowField(true) -> save($data,$data['id'])) {
				return $this -> toJson('200', '保存成功');
			} else {
				return $this -> toJson('406', '保存失败');
			}
		}else{
			return $this -> toJson('408', '请求错误');
		}
	}

	/**
	 * 地址删除
	 */
	public function del() {
		if ($this -> request -> isPost()) {
			$data = $this -> request -> post();
			$json = db('mall_address') -> where("id=" . $data['id']) -> delete();
			if ($json) {
				return $this -> toJson('200', '删除成功');
			} else {
				return $this -> toJson('406', '删除失败');
			}
		}
	}

}
